package leetcode;

import java.util.HashSet;
import java.util.Set;

/**
 * @program: datastructureandalogorithm
 * @description:
 * @author: hmx
 * @create: 2021-10-18 16:14
 **/
public class LeetCode349 {

    /**
     * 使用了两个Set集合解决交集问题
     * @param nums1
     * @param nums2
     * @return
     */
    public int[] intersection(int[] nums1, int[] nums2) {
        Set<Integer> set = new HashSet<>();
        Set<Integer> set2 = new HashSet<>();

        for (int i : nums1) {
            set.add(i);
        }

        for (int i : nums2) {
            if (set.contains(i)) {
                set2.add(i);
            }
        }
        int[] nums3 = new int[set2.size()];
        int count = 0;
        for (Integer integer : set2) {
            nums3[count++] = integer;
        }
        return nums3;
    }

}
